package com.spbl.apache.shiro.config.session;


import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description(会话监听)
 * author: Gao xueyong
 * Create at: 2020/6/14 10:06 上午
 */
@Slf4j
public class CustomeSessionListener implements SessionListener {
    /**
     * 在线人数
     */
    private final AtomicInteger sessionCount = new AtomicInteger(0);

    /**
     * 会话创建触发
     *
     * @param session
     */
    @Override
    public void onStart(Session session) {
        sessionCount.incrementAndGet();
    }

    /**
     * 会话停止触发
     *
     * @param session
     */
    @Override
    public void onStop(Session session) {
        sessionCount.decrementAndGet();
    }

    /**
     * 会话失效触发
     *
     * @param session
     */
    @Override
    public void onExpiration(Session session) {
        log.debug("会话失效！");
        sessionCount.decrementAndGet();
    }

    /**
     * 获取在线人数
     * @return
     */
    public AtomicInteger getSessionCount() {
        return sessionCount;
    }
}
